# -*- coding: utf-8 -*-
"""
Author: chen xi
Date: 2025/5/9 下午2:51
File: retry_decorator.py
"""

import time
import functools


def retry_decorator(max_retries=2, delay=5):
	"""
    装饰器，用于在方法抛出异常后重试。

    :param max_retries: 最大重试次数
    :param delay: 每次重试之间的延迟时间（秒）
    """
	
	def decorator(func):
		@functools.wraps(func)
		def wrapper(*args, **kwargs):
			retries = 0
			while retries < max_retries:
				try:
					return func(*args, **kwargs)
				except Exception as e:
					retries += 1
					print(f"方法 {func.__name__} 抛出异常: {e}, 第 {retries} 次重试...")
					if retries >= max_retries:
						print(f"方法 {func.__name__} 达到最大重试次数，放弃重试")
						raise
					time.sleep(delay)  # 等待一段时间后重试
		
		return wrapper
	
	return decorator
